﻿<cfscript>
/** 
* @hint "acadmicBase - 开课学院的课程名称唯一性验证" 
*/ 

component 
	extends="MachII.framework.EventFilter" 
	displayname="acadmicManager.filters.senate.courseExistsFilter" 
	output=false 
	{
	
	/*
	* @hint "Configures this filter as part of the Mach-II framework" 
	*/
	public void function configure() output=false {} 
	
	/*
	* @hint "Filters event and returns a boolean to Mach-II indicating whether or not the event queue should proceed.  If not, the event queue is cleared and a new event is announced."
	* 
	* @event "MachII Event Object"
	* @eventContext "MachII EventContext Object"
	*/
	public boolean function filterEvent ( required MachII.framework.Event event, required MachII.framework.EventContext eventContext ) output=false {
		
		var courseId = event.getArg("CourseID");
		var courseCode = event.getArg("CourseCode");
		
		var rs_check = queryNew("cid");
		if ( len(courseId) ) {
			var sql = "SELECT a.cid 
						FROM t_course a  
						WHERE (a.cid > :courseId 
							  or 
							  a.cid < :courseId ) 
							  and 
							  a.course_code = :courseCode ";
			var queryObj = new Query( datasource=application.dnsSlave ); 
			queryObj.addParam( name="courseCode", value=courseCode, cfsqltype="cf_sql_varchar" );
			queryObj.addParam( name="courseId", value=courseId, cfsqltype="cf_sql_varchar" );
			rs_check = queryObj.execute( sql=sql ).getResult();
		}
		else {
			var sql = "SELECT a.cid 
						FROM t_course a  
						WHERE 
							  a.course_code = :courseCode ";
			var queryObj = new Query( datasource=application.dnsSlave ); 
			queryObj.addParam( name="courseCode", value=courseCode, cfsqltype="cf_sql_varchar" );
			rs_check = queryObj.execute( sql=sql ).getResult();
		}
		
		if (rs_check.recordCount) {
			/* 如果已经存在同学院开设的相同课程 并且课程号与当前不同 */
			event.setArg("courseExistsID", valueList(rs_check.cid,","));
			event.setArg("TabID", "labCreate");
			announceEvent("courseExists", event.getArgs());
		}
		else {
			/* 如果课程和开课单位的组合不存在 则继续更新 */
			return true;
		}
		
		return false;
	}
	
}

</cfscript>